package cn.com.Servlet.ArticleServlet;
import cn.com.controler.Article.ArticleDAO;
import cn.com.controler.Myarticle.MyarticleDAO;
import cn.com.controler.Util.TimeTr;
import cn.com.entity.Article;
import cn.com.entity.Myarticle;
import cn.com.entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "DataDMLServlet",value = "/artdataDML")
public class DataDMLServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String czlx = request.getParameter("czlx");
        if(czlx.equals("tj")){    //发布帖子
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute("user");
            Integer userId = user.getUserId();
            Date date = new Date();
            String s = TimeTr.Date_String(date);
            Article art = new Article(0,userId,0,title,content,s,0);
            ArticleDAO articleDAO = new ArticleDAO();
            int addarticle = articleDAO.addarticle(art);
            if(addarticle == 1){
                response.getWriter().write("发布成功");
            }else{
                response.getWriter().write("发布失败");
            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String type = request.getParameter("qqlx");
        if(type.equals("cxqb")){       //查询全部帖子
            ArticleDAO articleDAO = new ArticleDAO();
            List<Article> article = articleDAO.selectallarticle();
            HttpSession session = request.getSession();
            session.setAttribute("allart",article);
            response.sendRedirect("/WebContent/HTML/UserPage/user_article/ShowArticle.jsp");
        }else if(type.equals("gldt")){   //管理我的帖子
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute("user");
            Integer userId = user.getUserId();
            MyarticleDAO myarticleDAO = new MyarticleDAO();
            List<Myarticle> article = myarticleDAO.getallMyart(userId);
            request.setAttribute("list",article);
            request.getRequestDispatcher("/WebContent/HTML/UserPage/user_article/glarticle.jsp").forward(request,response);
        }else if(type.equals("ykcx")){   //游客查询帖子
            ArticleDAO articleDAO = new ArticleDAO();
            List<Article> article = articleDAO.selectallarticle();
            request.setAttribute("allart",article);
            request.getRequestDispatcher("/WebContent/HTML/TouristPage/ShowArticle.jsp").forward(request,response);

        }else if(type.equals("delete")){   //删除贴子
            int delid = Integer.valueOf(request.getParameter("delid"));
            ArticleDAO articleDAO = new ArticleDAO();
            int i = articleDAO.delarticleby(delid);
            if(i > 0){
                response.sendRedirect("/artdataDML?qqlx=gldt");
            }else{
                response.getWriter().write("删除失败");
            }
        }
    }
}
